package top.lgd.controller.operation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lgd.base.LgdConstants;
import top.lgd.base.LgdResult;
import top.lgd.base.LgdTable;
import top.lgd.controller.LgdController;
import top.lgd.pojo.entity.operation.Appointment;
import top.lgd.service.operation.AppointmentService;

/**
 * @author YYJ
 * @version 1.0
 * @since 2023/12/19 19:12
 */
@RestController
@RequestMapping("/admin/operationAppointment")
public class AppointmentAdminController extends LgdController {

    @Autowired
    private AppointmentService appointmentService;

    /**
     * 查询会员列表
     *
     * @return
     */
    @PostMapping("/queryList")
    public LgdTable<Appointment> queryList(@RequestBody Appointment appointment) {
        QueryWrapper<Appointment> queryWrapper = new QueryWrapper(appointment);
        if (null != appointment.getStartDate()) {
            queryWrapper.ge(LgdConstants.COLUMN_APPOINT_DATE, appointment.getStartDate());
        }
        if (null != appointment.getStartDate()) {
            queryWrapper.le(LgdConstants.COLUMN_APPOINT_DATE, appointment.getEndDate());
        }
        queryWrapper.eq(LgdConstants.COLUMN_DATA_USER_ID, current().getId())
                .orderByAsc(LgdConstants.COLUMN_STATUS)
                .orderByDesc(LgdConstants.COLUMN_APPOINT_DATE);
        Page<Appointment> page = appointmentService.page(new Page(appointment.getCurrent(), appointment.getPageSize()), queryWrapper);
        return getTable(page);
    }

    /**
     * 添加编辑学习内容
     *
     * @param appointment 学习内容 对象
     * @return
     */
    @PostMapping("addUpdate")
    public LgdResult<Appointment> addUpdate(@RequestBody Appointment appointment) {
        if (null != appointment.getId()) {
            UpdateWrapper<Appointment> updateWrapper = new UpdateWrapper();
            updateWrapper.eq(LgdConstants.COLUMN_ID, appointment.getId())
                    .eq(LgdConstants.COLUMN_DATA_USER_ID, current().getId());
            appointmentService.update(appointment, updateWrapper);
        } else {
            appointment.setDataUserId(current().getId());
            appointmentService.save(appointment);
        }
        return LgdResult.success(appointment);
    }
}